package com.demo.rabbitmq.direct;

import com.demo.rabbitmq.util.ConnectionUtil;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.nio.charset.StandardCharsets;

public class Producer {

    public static void main(String[] args) throws Exception{
        Connection connection = ConnectionUtil.getConnection();

        Channel channel = connection.createChannel();

        //交换机名称
        String exchangeName = "test_direct_queue";
        //声明交换机
        channel.exchangeDeclare(exchangeName, BuiltinExchangeType.DIRECT, true, false, false, null);

        //队列名称
        String queue1Name = "test_direct_queue1";
        String queue2Name = "test_direct_queue2";

        //声明队列
        channel.queueDeclare(queue1Name, true, false, false, null);
        channel.queueDeclare(queue2Name, true, false, false, null);

        //对交换机和队列进行绑定
        channel.queueBind(queue1Name, exchangeName, "error");

        channel.queueBind(queue2Name, exchangeName, "info");
        channel.queueBind(queue2Name, exchangeName, "error");
        channel.queueBind(queue2Name, exchangeName, "warning");

        String message = "日志信息:张三调用了delete方法，错误了，日志级别error";

        //发送消息
        channel.basicPublish(exchangeName, "error", null, message.getBytes(StandardCharsets.UTF_8));

        //释放资源
        channel.close();
        connection.close();

    }
}
